package com.atguigu.KMP;

/**
 * description:
 * Created by 才睡醒 on 2021-09-05
 */
public class ViolenceMatch {
    public static void main(String[] args) {
        String str1 = "硅硅谷 尚硅谷你尚硅 尚硅谷你尚硅谷你尚硅你好";
        String str2 = "尚硅谷你尚硅你";
        int index = violenceMatch(str1, str2);
        System.out.println(index);
    }

    public static int violenceMatch(String str1,String str2){
        //传入字符串准换为字符数组
        char[] s1 = str1.toCharArray();
        char[] s2 = str2.toCharArray();
        int i = 0;
        int j = 0;

        //判断是否匹配
        while (i <s1.length && j <s2.length){
            if(s1[i] == s2[j]){
                i++;
                j++;
            }else {
               i= i-j+1;
             //   i++;
                j=0;
            }
        }
        if(j==s2.length){
            return i-j;
        }else {
            return -1;
        }
    }
}
